$(function(){

    $("#pc").on('keydown',function(event){
        stop(event);
    })
    $("#pc").on('keyup',function(event){
        begin(event);
    })


	//axios本版本不支持jsonp 自己拓展一个
    axios.jsonp = (url) => {
        if(!url){
            console.error('Axios.JSONP 至少需要一个url参数!')
            return;
        }
        return new Promise((resolve,reject) => {
            window.jsonCallBack =(result) => {
                resolve(result)
            }
            var JSONP=document.createElement("script");
            JSONP.type="text/javascript";
            JSONP.src=`${url}?callback=jsonCallBack`;
            document.getElementsByTagName("head")[0].appendChild(JSONP);
            setTimeout(() => {
                document.getElementsByTagName("head")[0].removeChild(JSONP)
            },500)
        })
    }
    var vm = new Vue({
        el:'.cube-list-rank',
        data:{
            cubelistrank: [],
            search:""
        },
        
        mounted(){
                axios.jsonp('https://andy168.gitee.io/dctimer/data/qq_data2.js')
                .then(res => {
                    console.log(res); 
                    this.cubelistrank =res.lists;
                }                    )
                .catch(err => console.log(err))
                
                

        },
        methods: {
            getUname: function(a){
                $.ajax({
                    url: "http://www.d163.net/api/index/getqqInfo.html",
                    type:"GET",
                    data:{qq:a},
                    dataType:"json",
                    success:function(res){
                        console.log(res);
                        return res.name;
                    }

                })
            }
        },
        computed:{
			okcuberank:function(){
				return sortKey(this.cubelistrank,'result');
			},
            searchData: function() {
                sortKey(this.cubelistrank,'result');
                var search = this.search;
                if (search) {
                    // 按所有字段搜索
                    return this.cubelistrank.filter(function(cuber) {
                        return Object.keys(cuber).some(function(key) {
                            return String(cuber[key]).toLowerCase().indexOf(search) > -1
                        })
                    })

                    /* 按名字搜索
                    return this.cubelistrank.filter(value => {
                        return value.uname.match(search)
                    })
                    */
            }
              
            return this.cubelistrank;
            }
		}
    })

	function sortKey(array,key){
		return array.sort(function(a,b){
		var x = a[key]*100;
		var y = b[key]*100;
		return ((x<y)?-1:(x>y)?1:0)
		})
	}
})